package hu.veilingsite.gui;

import java.util.ArrayList;

import hu.veilingsite.client.Service;
import hu.veilingsite.client.ServiceAsync;
import hu.veilingsite.client.Systeem;
import hu.veilingsite.shared.Auction;
import hu.veilingsite.shared.Bid;
import hu.veilingsite.shared.FieldVerifier;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;

public class ProfileBids {
	private Button BTN_terug;
	private final FlexTable biedingPanel = new FlexTable();
	final ServiceAsync myService = (ServiceAsync)GWT.create(Service.class);
	
	public ProfileBids() {
		AsyncCallback<ArrayList<Bid>> getBidsFromUser = new AsyncCallback<ArrayList<Bid>>(){
			@Override
			public void onFailure(Throwable caught) {
				RootPanel.get("main").clear();
				RootPanel.get("main").add(new HTML("Er is een fout opgetreden in het systeem."));
			}

			@Override
			public void onSuccess(ArrayList<Bid> biedingen) {
				biedingPanel.setWidget(0, 0, new Label("Al uw biedingen:"));
				if (biedingen.size() > 0) {
					biedingPanel.setWidget(1, 0, new Label("Geboden op:"));
					biedingPanel.setWidget(1, 1, new Label("Met credits:"));
					biedingPanel.setWidget(1, 2, new Label("Op datum:"));
					for (int i = 0; i < biedingen.size(); i++) {
						Bid bid = biedingen.get(i);
						setAuctionName(i+2, bid.getAuction_id());
						biedingPanel.setWidget(i+2, 1, new Label("" + bid.getCredits()));
						biedingPanel.setWidget(i+2, 2, new Label("" + bid.getDate()));
						final int aucId = bid.getAuction_id();
						final int bidId = bid.getId();
						// bekijkhandler
						ClickHandler bekijkHandler = new ClickHandler() {
							@Override
							public void onClick(ClickEvent event) {
								RootPanel.get("main").clear();
								RootPanel.get("main").add(new ViewAuction(aucId).createPage());
							}
						};
						// annuleerHandler
						ClickHandler annuleerHandler = new ClickHandler() {
							@Override
							public void onClick(ClickEvent event) {
								annuleerBieding(bidId);
							}
						};
						biedingPanel.setWidget(i+2, 3, new Button("Bekijk",bekijkHandler));
						biedingPanel.setWidget(i+2, 4, new Button("Annuleer",annuleerHandler));
					}
				} else {
					biedingPanel.setWidget(1, 0, new Label("U heeft nog geen biedingen gedaan."));
				}
				RootPanel.get("main").add(biedingPanel);
				BTN_terug = new Button("Terug");
				BTN_terug.addClickHandler(new ClickHandler() {
					public void onClick(ClickEvent e) {
						RootPanel.get("main").clear();
						RootPanel.get("main").add(new Profile().createPage());
					}
				});
			}
			public void setAuctionName(final int row, int aucId){
				AsyncCallback<Auction> getAuction = new AsyncCallback<Auction>(){
					@Override
					public void onFailure(Throwable caught) {
						RootPanel.get("main").clear();
						RootPanel.get("main").add(new HTML("Er is een fout opgetreden in het systeem."));
					}

					@Override
					public void onSuccess(Auction auction) {
						biedingPanel.setWidget(row, 0, new Label(auction.getTitle()));
					}
				};
				myService.getAuction(aucId, getAuction);
			}
			public void annuleerBieding(int bidId){
				AsyncCallback<Boolean> deleteBid = new AsyncCallback<Boolean>(){
					@Override
					public void onFailure(Throwable caught) {
						RootPanel.get("main").clear();
						RootPanel.get("main").add(new HTML("Er is een fout opgetreden in het systeem."));
					}

					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							FieldVerifier.showMessageBox("Geannuleert", "Uw bieding is succesvol geannuleerd.", true);
							RootPanel.get("main").clear();
							RootPanel.get("main").add(new ProfileBids().createPage());
						} else {
							FieldVerifier.showMessageBox("Oops!", "Er is iets fout gegaan, probeer het opnieuw.", true);
						}
					}
				};
				myService.deleteBid(bidId, deleteBid);
			}
		};
		myService.getBidsFromUser(Systeem.ingelogd.getId(), getBidsFromUser);
	}
	
	public Panel createPage() {
		return null;
	}
}
